//
//  WelcomeViewController.m
//  CardWarrior
//
//  Created by Tekka Xie on 12-4-18.
//  Copyright (c) 2012年 Tencent. All rights reserved.
//

#import "WelcomeViewController.h"
#import "AudioManager.h"
#import "CardImageManager.h"

@interface WelcomeViewController()
{
@private
    UIImageView*  cardSequence[8];
}
@end


@implementation WelcomeViewController

@synthesize welcomeScreen;
@synthesize welcomeScene;
@synthesize starBackgroundA;
@synthesize starBackgroundB;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
    
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    @autoreleasepool {
    for (int i = 100; i <= 107; i++)
        cardSequence[i-100] = (UIImageView*)[self.view viewWithTag:i];
    
    srand(time(0));
    for (int i = 101; i <= 106; i++)
        [cardSequence[i-100] setImage:[[CardImageManager sharedInstance] GetImage:rand()%52]];

    // 3. welcomeScene 渐入
    void (^welcomeSceneEaseInSteperBlock)(BOOL finished);
    welcomeSceneEaseInSteperBlock = ^(BOOL finished){
        [UIView animateWithDuration: 0.1  delay: 0.0
                            options: UIViewAnimationOptionCurveEaseIn
                         animations: ^(){
                             welcomeScene.alpha = 1.0;
                         }
                         completion: nil
        ];
        
        starBackgroundA.frame = CGRectMake(0, 0, 480, 320);
        starBackgroundB.frame = CGRectMake(0, 320, 480, 320);
        [UIView animateWithDuration: 10.0  delay: 0.0
                            options: UIViewAnimationOptionCurveLinear|UIViewAnimationOptionRepeat
                         animations: ^(){
                             starBackgroundA.frame = CGRectMake(0, -320, 480, 320);
                             starBackgroundB.frame = CGRectMake(0, 0, 480, 320);
                         }
                         completion: nil
         ];
        
        [[AudioManager sharedInstance] PlayBGM:@"WelcomeBGM"];
    };
    
    // 2. welcomeScreen 渐出
    void (^welcomeScreenEaseOutSteperBlock)(BOOL finished);
    welcomeScreenEaseOutSteperBlock = ^(BOOL finished){
        [UIView animateWithDuration: 2.0  delay: 2.0
                            options: UIViewAnimationOptionCurveEaseOut
                         animations: ^(){
                             welcomeScreen.alpha = 0.0;
                         }
                         completion: welcomeSceneEaseInSteperBlock
         ];
    };
    
    // 1. welcomeScreen 渐入
    [UIView animateWithDuration: 1.0  delay: 0.0
                        options: UIViewAnimationOptionCurveEaseIn
                     animations: ^(){
                         welcomeScreen.alpha = 1.0;
                     }
                     completion: welcomeScreenEaseOutSteperBlock    
     ];
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if (interfaceOrientation == UIInterfaceOrientationLandscapeRight
        || interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
        return YES;
    
    return NO;
}

#pragma mark - touches

static CGPoint startPoint;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    @autoreleasepool {
    UITouch *touch = [[event allTouches] anyObject];
	CGPoint touchPoint = [touch locationInView:self.view];
    
    if (CGRectContainsPoint(cardSequence[0].frame, touchPoint)
     || CGRectContainsPoint(cardSequence[1].frame, touchPoint))
    {
        startPoint = touchPoint;
    }
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    @autoreleasepool {
    UITouch *touch = [[event allTouches] anyObject];
	CGPoint touchPoint = [touch locationInView:self.view];
    
    if (touchPoint.x - startPoint.x > 35)
    {
        [NSTimer scheduledTimerWithTimeInterval:0.1
                                        target:self
                                      selector:@selector(timerFireMethod:)
                                      userInfo:nil
                                       repeats:YES];
    }
    }
}

- (void)timerFireMethod:(NSTimer*)theTimer
{
    @autoreleasepool {
    static int i = 101;

    if (i < 107)
    {
        int index = i-100;
        [UIView transitionWithView:cardSequence[index] 
                          duration:0.5 
                           options:UIViewAnimationOptionTransitionFlipFromLeft
                        animations:^{
                            [cardSequence[index] setImage: cardSequence[0].image];
                        } 
                        completion:nil];
        if (i == 101)
            [[AudioManager sharedInstance] PlaySFX:@"SlideSFX"];
        i++;
    }
    else
    {
        [theTimer invalidate];
        
        [self performSegueWithIdentifier:@"SegueToMainViewController" sender:self];
    }
    }
}

@end
